package com.tenebrous.jestic.controller.framwork.system.schedule;

import com.tenebrous.jestic.compents.annotations.Weblog;
import com.tenebrous.jestic.compents.annotations.WeblogEnum;
import com.tenebrous.jestic.controller.SupperController;
import com.tenebrous.jestic.entity.framwork.meber.organ.OrgUser;
import com.tenebrous.jestic.entity.framwork.system.SysSchedule;
import com.tenebrous.jestic.service.framwork.system.SysScheduleService;
import com.rain.ops.sdk.dto.ApiResult;
import com.rain.ops.sdk.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/schedule")
public class ScheduleController extends SupperController {

    @Autowired
    private SysScheduleService scheduleService;

    @PostMapping("/get")
    public ApiResult get(@NotBlank String id) {
        SysSchedule item = scheduleService.selectByPrimaryKey(id);
        if (item != null) {
            return new ApiResult(200, "查询成功。", item);
        }
        return new ApiResult(999, "查询失败，未找到指定记录。");
    }

    @PostMapping("/key/get")
    public ApiResult getByKey(@NotBlank String key) {
        OrgUser ouser = super.getCurrentUser();
        SysSchedule item = scheduleService.getByStime(key, ouser.getId());
        if (item != null) {
            return new ApiResult(200, "查询成功。", item);
        }
        return new ApiResult(999, "查询失败，未找到指定记录。");
    }

    @PostMapping("/key/query")
    public ApiResult queryByKey(@NotBlank String key) {
        OrgUser ouser = super.getCurrentUser();
        List<SysSchedule> item = scheduleService.queryByStime(key, ouser.getId());
        if (item != null) {
            return new ApiResult(200, "查询成功。", item);
        }
        return new ApiResult(999, "查询错误。");
    }

    @PostMapping("/query")
    public ApiResult query() {
        Calendar cal = Calendar.getInstance();
        OrgUser ouser = super.getCurrentUser();
        String syear = String.format("%d", cal.get(Calendar.YEAR));
        String smonth = String.format("%02d", cal.get(Calendar.MONTH) + 1);
        List list = scheduleService.queryList(syear, smonth, ouser.getId());
        return new ApiResult(200, "查询成功。", list);
    }

    /**
     * 更新或保存日程安排
     *
     * @param item
     * @return
     */
    @PostMapping("/supdate")
    @Weblog(description = "更新或保存日程安排", method = WeblogEnum.UPDATE)
    public ApiResult saveupdate(@NotNull SysSchedule item) {
        Date dt = new Date();
        OrgUser ouser = super.getCurrentUser();
        if (StringUtils.isNotBlank(item.getId())) {
            item.setUpdateDate(dt);
            item.setUpdateUserid(ouser.getId());
            scheduleService.updateSelective(item);
        } else {
            String stime = item.getStime();
            if (StringUtils.isNotBlank(stime)) {
                String syear = stime.substring(0, 4);
                String smonth = stime.substring(4, 6);
                String sday = stime.substring(6, 8);
                item.setSyear(syear);
                item.setSmonth(smonth);
                item.setSday(sday);
            }
            item.setId(UUIDUtils.randomUUID());
            item.setCreateDate(dt);
            item.setSort(dt.getTime());
            item.setCreateUserid(ouser.getId());
            scheduleService.saveSelective(item);
        }
        return new ApiResult(200, "更新成功。", item);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @Weblog(description = "批量删除日程安排记录", method = WeblogEnum.DELETE)
    public ApiResult delete(@NotEmpty String[] ids) {
        return new ApiResult(200, "删除成功。", scheduleService.deleteByIds(ids));
    }

    /**
     * 更新记录排序值
     *
     * @param id
     * @param sort
     * @return
     */
    @PostMapping("/sort/update")
    @Weblog(description = "更新记录排序值", method = WeblogEnum.UPDATE)
    public ApiResult updateSort(@NotBlank String id, @NotNull Long sort) {
        SysSchedule item = scheduleService.selectByPrimaryKey(id);
        if (item != null) {
            item.setSort(sort);
            scheduleService.updateSelective(item);
            return new ApiResult(200, "更新成功。", item);
        }
        return new ApiResult(999, "更新失败，未找到指定记录。");
    }
}
